package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Role;
import com.example.demo.entity.RoleUserMap;
import com.example.demo.entity.User;
import com.example.demo.entity.UserRole;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.RoleUserMapMapper;
import com.example.demo.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;


    @Override
    public IPage<Role> selectPage(Integer pageNum, Integer pageSize, String roleCode, String roleName) {
        QueryWrapper<Role> queryWrapper =  new QueryWrapper<>();
        Page<Role> page = new Page<>(pageNum,pageSize);
        if (StrUtil.isNotBlank(roleCode)){
            queryWrapper.lambda().like(Role::getRoleCode,roleCode);
        }
        if (StrUtil.isNotBlank(roleName)){
            queryWrapper.lambda().like(Role::getRoleName,roleName);
        }
        return roleMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Result<?> addRole(Role role) {
        role.setDeleted(0);
        roleMapper.insert(role);
        return Result.success();
    }

    @Override
    public Result<?> updateRole(Role role) {
        roleMapper.updateById(role);
        return Result.success();
    }

    @Override
    public Result<?> deleteRole(Integer roleId) {
        roleMapper.deleteById(roleId);
        return Result.success();
    }

    @Override
    public IPage<User> selectUserByRoleId(Integer pageNum, Integer pageSize, Integer roleId,String username,String phoneNumber) {
        Page<User> page = new Page<>(pageNum,pageSize);
        return roleMapper.selectUserByRoleId(page,roleId,username,phoneNumber);
    }


}
